/**
 * PushSender.java 
 * Copyright © 2009-2014, Liu Tao
 * 
 * @author Liu Tao
 * @create 2014年5月22日
 */
package com.pshare.sender;

import java.util.List;

import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.json.JSONException;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotifications;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.pshare.constant.SeaxConstants;
import com.pshare.tool.ApnsPush;
import com.pshare.util.SysPropertyUtils;

/**
 * 推送者
 */
public abstract class PushSender {
	
	private static final Logger logger = Logger.getLogger(PushSender.class);
	
	protected final String apnsKeystore = SysPropertyUtils.getProperty("apns.keystore"); // "keystore.p12"
	protected final String apnsPassword = SysPropertyUtils.getProperty("apns.password");

	protected final String pushStartTime = SysPropertyUtils.getProperty("push.start.time");
	protected final String pushEndTime = SysPropertyUtils.getProperty("push.end.time");
	
	protected static final String DEFAULT_SOUND = "default";
	
	protected final Integer androidPushType = Integer.parseInt("1");
	protected final String androidPushUrl = SysPropertyUtils.getProperty("push.android.url");
	protected String androidAppKey = SysPropertyUtils.getProperty("push.android.appkey");
	protected String androidMasterSecret = SysPropertyUtils.getProperty("push.android.app_master_secret");
	
	protected Integer appType = 1;

	/**
	 * 安卓推送接口
	 * @param type 推送类型：广播、单播、组播等
	 * @param deviceToken
	 * @param message 消息文本
	 * @param objId 传递的业务对象ID
	 * @param msgType 自定义消息类型
	 * @param sound 自定义声音
	 * @throws SeaxRuntimeException
	 */
	public abstract void doPush(Integer type, List<String> deviceTokens, String title, String message, String objId, Integer msgType, String sound);
	
	/**
	 * 调用iOS APNS推送
	 * @param message 消息文本
	 * @param objId 需要传递的对象ID
	 * @param deviceTokens
	 * @param bizType 自定义消息类型
	 * @param sound 自定义提醒声音
	 */
	protected void apnsPush(String message, String objId, List<String> deviceTokens, Integer bizType, String sound) {
		if (message.length() >= 64) {
			message = message.substring(0, 60) + "...";
	    }
		try {
			PushNotificationPayload payload = PushNotificationPayload.complex();
			payload.addAlert(message);
			payload.addCustomDictionary("type", bizType);
			if(StringUtils.isNotBlank(objId)) {
				payload.addCustomDictionary("obj_id", objId);
			}
			if (StringUtils.isNotBlank(sound)) {
				payload.addSound(sound);
			} else {
				payload.addSound(DEFAULT_SOUND);
			}
			
			String keystores = getApnskeystore();
			// 同时包含正式和测试的APP，需要配置正式和测试的keystore文件
			String[] keystoreArray = keystores.split(SeaxConstants.COMMA);
			boolean isProduct = false;
			for (String keystore : keystoreArray) {
				if (keystore.endsWith(SeaxConstants.PUSH_APNS_TEST_END)) {
					isProduct = false;
				} else {
					isProduct = true;
				}
				PushedNotifications notifications = ApnsPush.payload(payload, keystore, getApnspassword(), isProduct, deviceTokens);
				logger.info(notifications);
			}
			logger.info("--------------complete sending apns msg--------------------");
			logger.info(deviceTokens + message);
		}catch(KeystoreException | CommunicationException |JSONException e){
			logger.error(e.getMessage(), e);
		}
 
	}
	
	protected String getApnskeystore() {
		return apnsKeystore;
	}

	protected String getApnspassword() {
		return apnsPassword;
	}

	public Integer getAppType() {
		return appType;
	}
	
}
